{
    "componentChunkName": "component---src-templates-blog-post-js",
    "path": "/different-types-in-typescript-and-how-to use-it/",
    "result": {"data":{"site":{"siteMetadata":{"title":"CrewCode Solutions"}},"markdownRemark":{"id":"3208aec9-c67c-5dd5-9859-9ec75308591f","excerpt":"In this article i will explain you how to use different typescript types 1. Core Types Number In typescript all the numbers are float by default, in javascript…","html":"<p>In this article i will explain you how to use different typescript types</p>\n<h4>1. Core Types</h4>\n<h5>Number</h5>\n<p>In typescript all the numbers are float by default, in javascript or typescript there is no difference between 5 and 5.0 they are all same</p>\n<pre><code class=\"language-ts\">const number = 5;\nconst number2 = 2.8;\n</code></pre>\n<h5>Boolean</h5>\n<pre><code class=\"language-ts\">const boolresult = true;\n\nif (boolresult) {\n  console.log(\"Inside if\");\n}\n</code></pre>\n<h5>Strings</h5>\n<pre><code class=\"language-ts\">const strresult = \"This is string constant variable\";\n</code></pre>\n<p>In all the above type typescript uses type inference to decide what type of variable based on the value we have assigned we can also manually define the type as below</p>\n<pre><code class=\"language-ts\">const number: number = 5;\nconst boolresult: boolean = true;\nconst strresult: string = \"This is string constant variable\";\n</code></pre>\n<h4>2. Object Types</h4>\n<p>In this example we are just telling typescript object that the variable is of type object</p>\n<pre><code class=\"language-ts\">const person: object = {\n  name: \"Abc\",\n  age: 50,\n  hobbies: [\"sdfsd\"],\n  role: [1, \"abc\"],\n};\n</code></pre>\n<p>In this example we are gonna tell typescript what is the type of properties that typescript object will hold</p>\n<pre><code class=\"language-ts\">const person1: {\n  name: string;\n  age: number;\n  hobbies: string[];\n  role: [number, string];\n} = {\n  name: \"Abc\",\n  age: 30,\n  hobbies: [\"sdfsd\", \"sdfsdf\"],\n  role: [1, \"abc\"],\n};\n</code></pre>\n<h4>3. Array Types</h4>\n<p>In this example typescript uses typeinference to detect what type of variable hobbies is</p>\n<pre><code class=\"language-ts\">const hobbies = [\"Cooking\", \"Singing\"];\n</code></pre>\n<p>In this we have manually tell the typescript that hobbies variable is of type array which holds string value</p>\n<pre><code class=\"language-ts\">const hobbies: string[] = [\"Cooking\", \"Singing\"];\n</code></pre>\n<h4>4. Tuple</h4>\n<p>Tuple is fixed length and fixed type array in typescript</p>\n<p>In this example you would notice that we have define the type inside array this mark that we will have only two element\nand they will be of type number and string</p>\n<pre><code class=\"language-ts\">const role: [number, string] = [1, \"abc\"];\n</code></pre>\n<h4>5. Enum</h4>\n<pre><code class=\"language-ts\">const ADMIN = \"ADMIN\";\nconst AUTHOR = \"AUTHOR\";\nconst READ_ONLY = \"READ_ONLY\";\n\nenum ROLE {\n  ADMIN,\n  AUTHOR,\n  READ_ONLY,\n}\n\nconsole.log(ROLE.ADMIN);\n</code></pre>\n<h4>6. ANY Type</h4>\n<p>Any type when used cancel the type check and can store any type of value inside variable for example</p>\n<pre><code class=\"language-ts\">const role: any[] = [\"Abc\", 1];\n</code></pre>\n<h4>7. Union Type</h4>\n<p>Union type variable helps us where we want any varibale to accept one or more type of values for example</p>\n<pre><code class=\"language-ts\">const uniontype: string | number | boolean = \"String var\";\nuniontype = 10;\nuniontype = true;\n</code></pre>\n<h4>8. Literal Types</h4>\n<pre><code class=\"language-ts\">const literaltype: \"as-text\" | \"as-number\" = \"as-text\";\n</code></pre>\n<h4>9. Type Aliases</h4>\n<p>In below we have defined a type alias for our union type and we are refeering that alias in the constant variable</p>\n<pre><code class=\"language-ts\">type combinable = number | string | boolean;\n\nconst combinablevar: combinable = \"hello alias\";\n</code></pre>\n<pre><code class=\"language-ts\">type User = { name: string; age: number };\nconst u1: User = { name: \"abc\", age: 30 };\n</code></pre>\n<h4>10. Function Types and Return Types</h4>\n<p>In this example we have defined function return type as a number</p>\n<pre><code class=\"language-ts\">function add(n1: number, n2: number): number {\n  return n1 + n2;\n}\n</code></pre>\n<p>In this example we have defined function return type as void</p>\n<pre><code class=\"language-ts\">function printResult(num: number): void {\n  console.log(\"Hello\" + num);\n}\n</code></pre>\n<p>In this example we are declaring a variable which is of type function</p>\n<pre><code class=\"language-ts\">let functiontype: Function;\n</code></pre>\n<p>In this example we have defined function types but we are specific about what type of argument it will recieve and\nwhat will be the return type</p>\n<pre><code class=\"language-ts\">function add(n1: number, n2: number): number {\n  return n1 + n2;\n}\nlet functiontype: (num1: number, num2: number) => number;\n\nfunctiontype = add(5, 2);\n</code></pre>\n<h4>11. Function callback type</h4>\n<pre><code class=\"language-ts\">function addAndHandler(n1: number, n2: number, cb: (num: number) => void) {\n  const result = n1 + n2;\n  cb(result);\n}\naddAndHandler(1, 2, (result) => {\n  console.log(\"Result\", result);\n});\n</code></pre>\n<h4>12. Unknown type</h4>\n<p>In unknown type we can store any type of values for example</p>\n<pre><code class=\"language-ts\">let unknowntype: unknown;\nunknowntype = 5;\nunknowntype = \"Abc\";\n</code></pre>\n<p>The difference between unknown type and any type is you cann't assign any other type variable to unknow type variable but in case of any you can</p>\n<pre><code class=\"language-ts\">let userInput: unknown;\nlet userName: string;\n\nuserInput = 5;\n\n//this will throw error as unknow type can be or cann't be of type string\n//but in case of any this will work\nuserName = userInput;\n</code></pre>\n<h4>13 Generic Types</h4>\n<p>Generic type is a type which is connected with some other type</p>\n<p>In this example we have defined generic array type which hold string value</p>\n<pre><code class=\"language-js\">//generic way of defining\nconst names: Array&#x3C;string | number | boolean> = [];\n\n//similar to above\nconst names: sting[] = [];\n</code></pre>\n<p>In this example we have defined a promise of generic type, we are sepcifying that this promise will return number type</p>\n<pre><code class=\"language-ts\">const promise: Promise&#x3C;number> = new Promise((resolve, reject) => {\n  setTimeout(() => {\n    resolve(10);\n  }, 2000);\n});\n</code></pre>\n<p>In this example we are defining a function which has generic type</p>\n<pre><code class=\"language-ts\">function merge&#x3C;T, U>(objA: T, objB: U) {\n  return Object.assign(objA, objB);\n}\n\nconst mergeObj = merge&#x3C;{ name: string; hobbies: string[] }, { age: number }>(\n  { name: \"Max\", hobbies: [\"sdfsd\"] },\n  { age: 30 }\n);\n</code></pre>","fields":{"slug":"/different-types-in-typescript-and-how-to use-it/"},"frontmatter":{"title":"Different Typescript types and how to use it","date":"July 10, 2022","description":"In this article i will explain you different typescript types and how to use it","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/typescript-types-and-usage.jpeg"}},"previous":{"fields":{"slug":"/deploying-react-app-to-firebase/"},"frontmatter":{"title":"Deploying react application to firebase","date":"June 30, 2022","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/react-to-firebase.jpeg"}},"next":{"fields":{"slug":"/docker-setup-for-express-react-mongodb-application/"},"frontmatter":{"title":"Docker setup for your MERN (MongoDB, Node, Express, React) application","date":"July 25, 2022","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/docker.jpg"}}},"pageContext":{"id":"3208aec9-c67c-5dd5-9859-9ec75308591f","previousPostId":"86b697c0-5c6d-5354-a352-a0f6e7e611c0","nextPostId":"c7eae7dc-7c82-5b16-b22a-ed80c892c31e"}},
    "staticQueryHashes": ["3860684146"]}